#include "../../include/Dict/dictionary.h"

#include<string>
#include<string.h>
#include<fstream>
#include<iostream>
#include<vector>
#include<unordered_map>
#include<unordered_set>
#include<set>
#include<dirent.h>
#include<sstream>

using std::string;
using std::vector;
using std::unordered_map;
using std::unordered_set;
using std::pair;
using std::set;
using std::cout;
using std::cin;
using std::endl;
using std::ios;
using std::ifstream;
using std::ofstream;
using std::istringstream;



void Dictionary::getFile()
{
    DIR *pdir = opendir(_path.c_str());
    if(!pdir)
    {
        perror("opendir");
        return;
    }
    struct dirent *ptr;

    while((ptr=readdir(pdir))!=0)
    {
        if(strcmp(ptr->d_name,".")!=0 && strcmp(ptr->d_name,"..")!=0)
        {
            _files.push_back(_path+"/"+ptr->d_name);
        }
    }
    closedir(pdir);

    for(auto &filename :_files)
    {
        //cout<<filename;
        build(filename.c_str());
    }

    store();
}
void Dictionary::preHandle(string & line)
{
    for(auto &ch :line)
    {
        if(!isalpha(ch))
        {
            ch=' ';
        }
        else if(isupper(ch))
        {
            ch=tolower(ch);
        }
    }
}

void Dictionary::build(const char* filename)
{
    /* cout<<"build"<<endl; */
    ifstream ifs(filename);
    if(!ifs)
    {
        cout<<"ifstream open"<<filename<<"failed"<<endl;//日志
        return;
    }

    ifstream ifs2("../data/stop_words_eng.txt");//文件路径待替换
    if(!ifs2)
    {
        cout<<"ifstream open stop_words_eng.txt failed"<<endl;//日志
        return;
    }


    string line;
    string word;
    size_t line_no=1;

    while(getline(ifs2,line))
    {
        preHandle(line);

        istringstream iss(line);

        while(iss>>word)
        {
            stop_word.insert(word);
        }

    }

    ifs2.close();


    while(getline(ifs,line))
    {

        preHandle(line);

        istringstream iss(line);

        while(iss>>word)
        {
            auto it=stop_word.find(word);
            if(it==stop_word.end()){
                _word[word]++;
            }
        }
    }
    ifs.close();

} 

void Dictionary::store()
{

    cout<<"store"<<endl;
    ofstream ofs1("../data/dictionary.data",ios::app);
    //ofs1.open("../data/dictionary.data",ios::app);//文件路径待替换
    if(!ofs1)
    {
        cout<<"ofstream open dictionary.data failed"<<endl;//日志
        return;
    }

    for(auto &elem :_word)
    {
        ofs1<<elem.first<<"  "<<elem.second<<endl;
    }

    ofs1.close();

    unordered_map<char,set<int>> _index;

     ifstream ifs1("../data/dictionary.data");
    if (!ifs1)
    {
        cout << "ifstream open dictionary failed" << endl;
    }

    string line;
    string word;
    string num;
    size_t line_no = 0;
    while (getline(ifs1, line))
    {

        istringstream iss(line);

        iss >> word;
        iss >> num;

        for (auto &ch : word)
        {
            _index[ch].insert(line_no);
        }

        line_no++;
    }

ofstream ofs("../data/index.data");
    if (!ofs)
    {
        cout << "ifstream open index.data failed" << endl;
    }

    for (auto &elem : _index)
    {
        ofs << elem.first << "     ";
        for (auto &index : elem.second)
        {
            ofs << index << "  ";
        }
        ofs << endl;
    }

    /* ofstream ofs("../data/index.data"); */
    /* if (!ofs) */
    /* { */
    /*     cout << "ifstream open index.data failed" << endl; */
    /* } */
    /* int index=1; */
    /* for (auto &elem : _word) */
    /* { */
    /*     for(auto &ch :elem.first) */
    /*     { */
    /*         ofs << ch << "     "; */
    /*         for (auto &index : elem.second) */
    /*         { */
    /*             ofs << index << "  "; */
    /*         } */
    /*         ofs << endl; */

        /* } */
        /* ofs << elem.first << "     "; */
        /* for (auto &index : elem.second) */
        /* { */
        /*     ofs << index << "  "; */
        /* } */
        /* ofs << endl; */
    /* } */

}

void test01(){ 
    Dictionary dict("../data/en_art"); 
    /* dict.getFile();
       dict.store(); */
}
int main() 
{ 
    test01();
    return 0;
}

